package com.powernode.money.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.powernode.money.constant.Constants;
import com.powernode.money.constant.Pager;
import com.powernode.money.model.User;
import com.powernode.money.service.*;
import com.powernode.money.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Reference(interfaceClass = LoanInfoService.class,version = "1.0.0")
    private LoanInfoService loanInfoService;
    @Reference(interfaceClass = UserService.class,version = "1.0.0")
    private UserService userService;
    @Reference(interfaceClass = BidInfoService.class,version = "1.0.0")
    private BidInfoService bidInfoService;
    @Reference(interfaceClass = FinanceAccountService.class,version = "1.0.0")
    private FinanceAccountService financeAccountService;
    @Reference(interfaceClass = RechargeRecordService.class,version = "1.0.0")
    private RechargeRecordService rechargeRecordService;
    @Reference(interfaceClass = IncomeRecordService.class,version = "1.0.0")
    private IncomeRecordService incomeRecordService;
    @GetMapping("/index")
    public String index(Model model , HttpSession session){
        //推送历史平均年化收益率
        model.addAttribute(Constants.HISTORY_AVERAGE_RATE,loanInfoService.queryHistoryAverageRate());
        model.addAttribute(Constants.ALL_USER_COUNT,userService.countUsers());
        model.addAttribute(Constants.ALL_BID_MONEY,bidInfoService.queryAllBidMoney());
        model.addAttribute("xLoanInfoList",loanInfoService.queryByProductTypeWithPage(Constants.PRODUCT_TYPE_X,1,1));
        model.addAttribute("uLoanInfoList", loanInfoService.queryByProductTypeWithPage(Constants.PRODUCT_TYPE_U, 1,4));
        model.addAttribute("sLoanInfoList", loanInfoService.queryByProductTypeWithPage(Constants.PRODUCT_TYPE_S, 1,8));

        User user = (User) session.getAttribute(Constants.SESSION_USER);
        if (user !=null){
            model.addAttribute("User",userService.queryByPrimaryKey(user.getId()));
            model.addAttribute("financeAccount",financeAccountService.queryByUid(user.getId()));
        }

        return "index";
    }

    @GetMapping("/myCenter")
    public String myCenter(Model model,HttpSession session){
        User user= (User) session.getAttribute(Constants.SESSION_USER);
        if (user!=null){
            System.out.println(user.getId());
            model.addAttribute("User",userService.queryByPrimaryKey(user.getId()));
            model.addAttribute("financeAccount",financeAccountService.queryByUid(user.getId()));
            model.addAttribute("rechargeRecord",rechargeRecordService.queryByUid(user.getId()));
            model.addAttribute("bidInfo",bidInfoService.queryByUid(String.valueOf(user.getId())));
            model.addAttribute("income",incomeRecordService.queryIncomeList(String.valueOf(user.getId())));
        }

        return "myCenter";
    }

    @GetMapping("/loan/myAccount")
    public String myAccount(Model model,HttpSession session){
        User user= (User) session.getAttribute(Constants.SESSION_USER);
        if (user!=null){
            System.out.println(user.getId());
            model.addAttribute("User",userService.queryByPrimaryKey(user.getId()));
            model.addAttribute("financeAccount",financeAccountService.queryByUid(user.getId()));
         }
        return "myAccount";
    }
    @GetMapping("/loan/myInvest")
    public String myInvest(@RequestParam Map<String, String> map,Model model,HttpSession session){
        User user= (User) session.getAttribute(Constants.SESSION_USER);
        if (user!=null){
            //model.addAttribute("myInvest",bidInfoService.queryByUid(String.valueOf(user.getId())));
            if (map.size() == 0 || map.get("currentPage").isEmpty()) {
                map.put("currentPage", "0");
            }
            // 查询用户列表 及设置分页信息
            List list = bidInfoService.queryByUid(String.valueOf(user.getId()));
            // 参数为当前页码、每页显示条数、查询的列表集合
            PageUtil pageInfo = new Pager(Integer.parseInt(map.get("currentPage")), 5, list);
            model.addAttribute("myInvest",pageInfo.getList());
            model.addAttribute("pageInfo", pageInfo);
        }
        return "myInvest";
    }
    @GetMapping("/loan/myIncome")
    public String myIncome(@RequestParam Map<String, String> map,Model model,HttpSession session){
        User user= (User) session.getAttribute(Constants.SESSION_USER);
        if (user!=null){
            if (map.size() == 0 || map.get("currentPage").isEmpty()) {
                map.put("currentPage", "0");
            }
            // 查询用户列表 及设置分页信息
            List list = incomeRecordService.queryIncomeList(String.valueOf(user.getId()));
            // 参数为当前页码、每页显示条数、查询的列表集合
            PageUtil pageInfo = new Pager(Integer.parseInt(map.get("currentPage")), 5, list);
            model.addAttribute("myIncome",pageInfo.getList());
            model.addAttribute("pageInfo", pageInfo);

        }
        return "myIncome";
    }
    @GetMapping("/loan/myRecharge")
    public String myRecharge(@RequestParam Map<String, String> map, Model model, HttpSession session){
        User user= (User) session.getAttribute(Constants.SESSION_USER);
        if (user!=null){
            if (map.size() == 0 || map.get("currentPage").isEmpty()) {
                map.put("currentPage", "0");
            }
            // 查询用户列表 及设置分页信息
            List list = rechargeRecordService.queryByUid(user.getId());
            // 参数为当前页码、每页显示条数、查询的列表集合
            PageUtil pageInfo = new Pager(Integer.parseInt(map.get("currentPage")), 5, list);
            model.addAttribute("myRecharge",pageInfo.getList());
            model.addAttribute("pageInfo", pageInfo);
        }
        return "myRecharge";
    }



}
